package com.jarvis.pzz.modules.shop.view;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jarvis.pzz.R;
import com.jarvis.pzz.common.AppStatus;
import com.jarvis.pzz.common.BaseActivity;
import com.jarvis.pzz.models.RunIndustryListModel;
import com.jarvis.pzz.models.SearchTypeModel;
import com.jarvis.pzz.models.ShopSaleTypeListModel;
import com.jarvis.pzz.models.ShopTypeListModel;
import com.jarvis.pzz.models.ShopsSearchModel;
import com.jarvis.pzz.modules.shop.adapter.ScreenAdapter;
import com.jarvis.pzz.widget.ScrollGridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ScreenActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.sg_shop_type)
    ScrollGridView sg_shop_type;
    @BindView(R.id.sg_shop_sales_type)
    ScrollGridView sg_shop_sales_type;
    @BindView(R.id.sg_runIndustry)
    ScrollGridView sg_runIndustry;

    @BindView(R.id.tv_commit)
    TextView tv_commit;
    @BindView(R.id.tv_reset)
    TextView tv_reset;
    @BindView(R.id.rel_left)
    RelativeLayout rel_left;

    ShopsSearchModel shopsSearchModel;

    ScreenAdapter shopTypeAdapter;
    ScreenAdapter shopSalesTypeAdapter;
    ScreenAdapter runIndustryAdapter;

    private List<SearchTypeModel> shopType_list = new ArrayList<>();
    private List<SearchTypeModel> shopSalesType_list = new ArrayList<>();
    private List<SearchTypeModel> runIndustry_list = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_screen;
    }

    @Override
    public void initData() {
        shopsSearchModel = (ShopsSearchModel) getIntent().getSerializableExtra("shopsSearchModel");

        for (int i = 0; i < shopsSearchModel.getShopTypeList().size(); i++) {
            ShopTypeListModel shopTypeListModel = shopsSearchModel.getShopTypeList().get(i);
            SearchTypeModel model = new SearchTypeModel(shopTypeListModel.getLabName(), shopTypeListModel.getLabId(), "shopType", false);
            shopType_list.add(model);
        }
        shopTypeAdapter = new ScreenAdapter(shopType_list, this);
        sg_shop_type.setAdapter(shopTypeAdapter);

        for (int i = 0; i < shopsSearchModel.getShopSaleTypeList().size(); i++) {
            ShopSaleTypeListModel shopSaleTypeListModel = shopsSearchModel.getShopSaleTypeList().get(i);
            SearchTypeModel model = new SearchTypeModel(shopSaleTypeListModel.getLabName(), shopSaleTypeListModel.getLabId(), "shopSaleType", false);
            shopSalesType_list.add(model);
        }
        shopSalesTypeAdapter = new ScreenAdapter(shopSalesType_list, this);
        sg_shop_sales_type.setAdapter(shopSalesTypeAdapter);

        for (int i = 0; i < shopsSearchModel.getRunIndustryList().size(); i++) {
            RunIndustryListModel runIndustryListModel = shopsSearchModel.getRunIndustryList().get(i);
            SearchTypeModel model = new SearchTypeModel(runIndustryListModel.getIndustryName(), runIndustryListModel.getIndustryName(), "shopRunIndustry", false);
            runIndustry_list.add(model);
        }
        runIndustryAdapter = new ScreenAdapter(runIndustry_list, this);
        sg_runIndustry.setAdapter(runIndustryAdapter);

    }

    @Override
    public void initView() {
        setStatusWhite(true);
        tv_commit.setOnClickListener(this);
        tv_reset.setOnClickListener(this);
        rel_left.setOnClickListener(this);
        sg_shop_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < shopType_list.size(); i++) {
                    SearchTypeModel searchTypeModel = shopType_list.get(i);
                    searchTypeModel.setSelect(false);
                }
                SearchTypeModel searchTypeModel = shopType_list.get(position);
                searchTypeModel.setSelect(true);
                shopTypeAdapter.notifyDataSetChanged();
            }
        });
        sg_shop_sales_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < shopSalesType_list.size(); i++) {
                    SearchTypeModel searchTypeModel = shopSalesType_list.get(i);
                    searchTypeModel.setSelect(false);
                }
                SearchTypeModel searchTypeModel = shopSalesType_list.get(position);
                searchTypeModel.setSelect(true);
                shopSalesTypeAdapter.notifyDataSetChanged();
            }
        });
        sg_runIndustry.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < runIndustry_list.size(); i++) {
                    SearchTypeModel searchTypeModel = runIndustry_list.get(i);
                    searchTypeModel.setSelect(false);
                }
                SearchTypeModel searchTypeModel = runIndustry_list.get(position);
                searchTypeModel.setSelect(true);
                runIndustryAdapter.notifyDataSetChanged();
            }
        });
    }

    private void AddOrChangeData(SearchTypeModel searchTypeModel) {
        boolean is_add = true;
        List<SearchTypeModel> list = AppStatus.getInstance().getList();
        for (int i = 0; i < list.size(); i++) {
            if (searchTypeModel.getType().equals(list.get(i).getType())) {
                is_add = false;
                list.set(i, searchTypeModel);
            }
        }
        if (is_add) {
            list.add(searchTypeModel);
        }
        AppStatus.getInstance().setList(list);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_left:
                finish();
                break;
            case R.id.tv_commit:
                for (int i = 0; i < shopType_list.size(); i++) {
                    SearchTypeModel searchTypeModel = shopType_list.get(i);
                    if (searchTypeModel.isSelect()) {
                        AddOrChangeData(searchTypeModel);
                    }
                }
                for (int i = 0; i < shopSalesType_list.size(); i++) {
                    SearchTypeModel searchTypeModel = shopSalesType_list.get(i);
                    if (searchTypeModel.isSelect()) {
                        AddOrChangeData(searchTypeModel);
                    }
                }
                for (int i = 0; i < runIndustry_list.size(); i++) {
                    SearchTypeModel searchTypeModel = runIndustry_list.get(i);
                    if (searchTypeModel.isSelect()) {
                        AddOrChangeData(searchTypeModel);
                    }
                }
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.tv_reset:
                for (int i = 0; i < shopType_list.size(); i++) {
                    SearchTypeModel searchTypeModel = shopType_list.get(i);
                    searchTypeModel.setSelect(false);
                    shopTypeAdapter.notifyDataSetChanged();
                }
                for (int i = 0; i < shopSalesType_list.size(); i++) {
                    SearchTypeModel searchTypeModel = shopSalesType_list.get(i);
                    searchTypeModel.setSelect(false);
                    shopSalesTypeAdapter.notifyDataSetChanged();
                }
                for (int i = 0; i < runIndustry_list.size(); i++) {
                    SearchTypeModel searchTypeModel = runIndustry_list.get(i);
                    searchTypeModel.setSelect(false);
                    runIndustryAdapter.notifyDataSetChanged();
                }

                break;

        }
    }
}
